# RecursivePalindrome.py # # Description: Calls the Palindrome function and prints # That’s a palindrome! # when the function returns True and # That’s no palindrome! # when then function returns False. # The program terminates when the user presses Enter # which creates an empty string. # Iterative and Recursive implementations. # # Author: Anne Lavergne # Date: March 4 2024 # Palindrome iterative function def palindrome(aWord): """Returns True if 'aWord' is a palindrome and False it is not.""" # Compute the index of the last character reverseIndex = len(aWord)-1 # Consider each character of first half of aWord for anIndex in range(0,len(aWord)//2): # If this character and it matching character # at the end of aWord or not the same character ... if aWord[anIndex] != aWord[reverseIndex]: # Then aWord is not a palindrome return False # Otherwise, consider the next pair of characters reverseIndex -= 1 # If we reached this point in the function # then aWord is a palindrome return True # Palindrome recursive function def palindromeR(aWord): """Returns True if 'aWord' is a palindrome and False it is not.""" # Base Case if len(aWord) <= 1: return True # Recursive Case # Check the first and last characters if aWord[0] != aWord[-1]: # aWord[len(aWord)-1] return False return palindromeR(aWord[1:-1]) # ***Main part of my program # Ask the user for a word, empty string to stop theWord = input("Please input a word or press Enter to stop: ") while theWord: # Call the iterative version of the function palindrome if palindrome(theWord): print("Calling palindrome(): That's a palindrome!") else: print("Calling palindrome(): That's no palindrome!") # Call the recursive version of the function palindrome if palindromeR(theWord): print("Calling palindromeR(): That's a palindrome!") else: print("Calling palindromeR(): That's no palindrome!") # Ask the user for a word, empty string to stop theWord = input("Please input a word or press Enter to stop: ") print("Bye!")